package com.lyf.lottery.util;

import com.lyf.lottery.model.po.OptActivityBlindBox;
import com.lyf.lottery.model.po.Prize;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author lyf95
 */
public class LotteryUtil {
    // 放大倍数
    private static final int mulriple = 1000000;

    public static Integer lottery(List<Prize> prizes) {
        int lastScope = 0;
        // 洗牌，打乱奖品次序
        Collections.shuffle(prizes);
        Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>();
        for (Prize prize : prizes) {
            int prizeId = prize.getPrizeId();
            // 划分区间
            int currentScope = lastScope + prize.getProbability().multiply(new BigDecimal(mulriple)).intValue();
            prizeScopes.put(prizeId, new int[]{lastScope + 1, currentScope});

            lastScope = currentScope;
        }

        // 获取1-1000000之间的一个随机数
        int luckyNumber = new Random().nextInt(mulriple);
        Integer luckyPrizeId = null;
        // 查找随机数所在的区间
        if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
            Set<Map.Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
            for (Map.Entry<Integer, int[]> m : entrySets) {
                int key = m.getKey();
                if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1]) {
                    luckyPrizeId = key;
                    break;
                }
            }
        }
        return luckyPrizeId;
    }
    public static List<Prize> packagePrizeList(List<OptActivityBlindBox> blindBoxList){
        List<Prize> list=new ArrayList<>();
        for (OptActivityBlindBox blindBox : blindBoxList) {
            Prize prize = new Prize();
            prize.setPrizeId(blindBox.getId());
            prize.setProbability(blindBox.getProbability());
            list.add(prize);
        }
        return list;
    }
}
